<?php

namespace App\Repositories\Auth;

use App\Lib\Utils\Category;
use App\Models\Auth\Permission;
use App\Repositories\Repository;


class PermissionRepository extends Repository
{


    /**
     *
     * 权限列表（二维数组）
     * author Paco
     * createTime 2019/2/3 15:06
     * @return \Illuminate\Http\JsonResponse|mixed
     */
    public function index()
    {
        $permissions = Permission::query()->get();
        if ($permissions) {
            $cat = new Category();
            $permissions = $cat->getTree($permissions,'pid',0,0,'pid','permission_id');
        }
        //调用分页方法（自定义数组分页）
        return $this->respondWith($permissions);
    }

    /**
     * 权限列表(多维数组)
     * @author: Pako
     * @Email: admin@jqcool.net
     * @Date: 18-10-9 下午9:43
     * @return mixed
     */
    public function list()
    {
        $permissions = Permission::query()->get();
        if ($permissions) {
            $cat = new Category();
            $permissions = $cat->getDeepTree($permissions,'pid','permission_id');
        }
        return $this->respondWith($permissions);
    }

    /**
     * 权限详情
     * @author: Pako
     * @Email: admin@jqcool.net
     * @Date: 18-10-9 下午9:44
     * @param $permission_id
     * @return mixed
     */
    public function show($permission_id)
    {
        $permission = Permission::query()->where("permission_id", "=", $permission_id)->first();
        return $this->respondWith($permission);
    }

    /**
     * 添加权限
     *
     * @author Pako
     * @createTime 2018/8/19 18:45
     * @param $data
     * @return mixed
     */
    public function store($data)
    {
        try {
            $data = Permission::query()->create($data);
            $this->log_done('添加权限', $data);
            return $this->respondWith($data);
        } catch (\Exception $e) {
            return $this->respondError(10108);
        }
    }

    /**
     *
     * 更新权限
     * @author Paco
     * @createTime 2018/8/20 12:11
     * @param $data
     * @return mixed
     */
    public function save($data)
    {
        $permission = Permission::query()->find($data['permission_id']);
        try {
            $permission->fill($data)->save();
            $this->log_done('更新权限', $data);
            return $this->respondWith($permission);
        } catch (\Exception $e) {
            return $this->respondError(10108);
        }
    }

    /**
     *
     * 删除权限
     * @author Paco
     * @createTime 2018/8/20 12:11
     * @param $permission_id
     * @return mixed
     */
    public function del($permission_id)
    {
        try {
            $data = Permission::query()->find($permission_id);
            Permission::query()->where("permission_id", "=", $permission_id)->delete();
            $this->log_done('删除权限', $data);
            return $this->respondSuccess();
        } catch (\Exception $e) {
            return $this->respondError(10107);
        }
    }


    /*
     * ==========================================================================================
     *                                    私有方法
     * ==========================================================================================
    */

}

